#include "judgement_info.h"
#include "communicate.h"
#include "comm_task.h"
#include "protocol.h"
#include "bsp_uart.h"
#include "data_fifo.h"
#include "string.h"

#include "shoot_task.h"

/* data send (forward) */
/* data receive */
common_judge_t judge_recv_mesg;
common_judge_t judge_send_mesg;

extern int8_t recv_pc_glb;
extern int8_t glb_err_exit;

/**
  * @brief    get judgement system message
  */
  
/*
	GAME_STATE_ID                = 0x0001,  //比赛状态数据
	GAME_RESULT_ID 	             = 0x0002,  //比赛结果数据
	ROBOT_SURVIVORS_ID           = 0x0003,	//比赛机器人存活数据
	EVENT_DATA_ID 				 = 0x0101,	//场地事件数据
	SUPPLY_PROJECTILE_ACTION_ID  = 0x0102,	//场地补给站动作标识数据
	SUPPLY_PROJECTILE_BOOK_ID    = 0x0103,	//场地补给站预约子弹数据
	GAME_ROBOT_STATE_ID          = 0x0201,	//机器人状态数据
	POWER_HEAT_DATA_ID           = 0x0202,	//实时功率热量数据
	ROBOT_POS_ID                 = 0x0203,	//机器人位置数据
	BUFF_MUSK_ID                 = 0x0204,	//机器人增益数据
	AERIAL_ROBOT_ENERGY_ID       = 0x0205,	//空中机器人能量状态数据
	ROBOT_HURT_ID                = 0x0206,	//伤害状态数据
	SHOOT_DATA_ID                = 0x0207,	//实时射击数据
	STUDENT_INTERACTIVE_ID        = 0x0301,	//机器人间交互数据
*/
  
extern TaskHandle_t pc_unpack_task_t;

uint8_t interover_data_overflow;

void judgement_data_handler(uint8_t *p_frame)
{
  frame_header_t *p_header = (frame_header_t*)p_frame;
  memcpy(p_header, p_frame, HEADER_LEN);

  uint16_t data_length = p_header->data_length;
  uint16_t cmd_id      = *(uint16_t *)(p_frame + HEADER_LEN);
  uint8_t *data_addr   = p_frame + HEADER_LEN + CMD_LEN;
  
  uint8_t forward_flag = 1;
  
  switch (cmd_id)
  {
    case GAME_STATE_ID:
    {
      memcpy(&judge_recv_mesg.game_state, data_addr, data_length);
       if (judge_recv_mesg.game_state.game_progress == 3)  //5 seconds count down
       {
         shoot.shoot_bullets = 0;
       }
      
      if (judge_recv_mesg.game_state.game_progress == 1)   //prepare stage
      {
        if (judge_recv_mesg.game_state.stage_remain_time < 240)
        {
          if (recv_pc_glb == 0)
          {
            glb_err_exit = 1;
          }
        }
      }
      else
      {
        recv_pc_glb  = 0;
        glb_err_exit = 0;
      }
    }
    break;

	  case GAME_RESULT_ID:
      memcpy(&judge_recv_mesg.game_result, data_addr, data_length);
    break;

    case ROBOT_SURVIVORS_ID:
      memcpy(&judge_recv_mesg.game_robot_survivors, data_addr, data_length);
    break;

    case EVENT_DATA_ID:
      memcpy(&judge_recv_mesg.event_data, data_addr, data_length);
    break;
    
    case SUPPLY_PROJECTILE_ACTION_ID:
      memcpy(&judge_recv_mesg.supply_projectile_action, data_addr, data_length);
    break;

    case SUPPLY_PROJECTILE_BOOK_ID:
      memcpy(&judge_recv_mesg.supply_projectile_booking, data_addr, data_length);
    break;

    case GAME_ROBOT_STATE_ID:
      memcpy(&judge_recv_mesg.game_robot_state, data_addr, data_length);
    break;
    
    case POWER_HEAT_DATA_ID:
      memcpy(&judge_recv_mesg.power_heat_data, data_addr, data_length);
    break;
	
		case ROBOT_POS_ID:
			memcpy(&judge_recv_mesg.game_robot_pos, data_addr, data_length);
		break;
		
		case BUFF_MUSK_ID:
			memcpy(&judge_recv_mesg.buff_musk, data_addr, data_length);
		break;
			
		case AERIAL_ROBOT_ENERGY_ID:
			memcpy(&judge_recv_mesg.aerial_robot_energy, data_addr, data_length);
		break;		
			
		case ROBOT_HURT_ID:
			memcpy(&judge_recv_mesg.robot_hurt, data_addr, data_length);
		break;		
			
		case SHOOT_DATA_ID:
			memcpy(&judge_recv_mesg.shoot_data, data_addr, data_length);
		break;			
		
		case STUDENT_INTERACTIVE_ID:
		{
			if(data_length <= 119)    //数据段头结构长度+交互数据长度
			{
				memcpy(&judge_recv_mesg.student_interactive_header_data, data_addr, data_length);
				interover_data_overflow = 0;
			}
			else
			{
				interover_data_overflow = 1;
			}
		}break;	

    default:
      forward_flag = 0;
    break;
  }
  
  /* forward data */
  if (forward_flag)
  {
    data_packet_pack(cmd_id, data_addr, data_length, UP_REG_ID);
    osSignalSet(pc_unpack_task_t, PC_UART_TX_SIGNAL);
  }
}

/**
  * @brief  Pack the client interactive data
  * @param  p_data:	pointer to a client_custom_data_t structure that
  *					store data.
  */

void judgement_client_packet_pack(uint8_t *p_data)
{	
	client_custom_data_t *frame = (client_custom_data_t*)p_data;
	memcpy(frame, p_data, sizeof(CLIENT_DATA_LENGTH));
	
	uint8_t current_robot_id = 0;
	uint16_t receiver_ID = 0;

//	uint8_t *puck_buff;
	
	current_robot_id = judge_recv_mesg.game_robot_state.robot_id;  //读取当前机器人的id
	switch(current_robot_id)
	{
		//red robot
		case STUDENT_RED_HERO_ID:
		{
			receiver_ID = RED_HERO_CLIENT_ID;
		}break;
		case STUDENT_RED_ENGINEER_ID:
		{
			receiver_ID = RED_ENGINEER_CLIENT_ID;
		}break;
		case STUDENT_RED_INFANTRY3_ID:
		{
			receiver_ID = RED_INFANTRY3_CLIENT_ID;
		}break;
		case STUDENT_RED_INFANTRY4_ID:
		{
			receiver_ID = RED_INFANTRY4_CLIENT_ID;
		}break;
		case STUDENT_RED_INFANTRY5_ID:
		{
			receiver_ID = RED_INFANTRY5_CLIENT_ID;
		}break;
		case STUDENT_RED_AERIAL_ID:
		{
			receiver_ID = RED_AERIAL_CLIENT_ID;
		}break;
		
		//blue robot
		case STUDENT_BLUE_HERO_ID:
		{
			receiver_ID = BLUE_HERO_CLIENT_ID;
		}break;
		case STUDENT_BLUE_ENGINEER_ID:
		{
			receiver_ID = BLUE_ENGINEER_CLIENT_ID;
		}break;
		case STUDENT_BLUE_INFANTRY3_ID:
		{
			receiver_ID = BLUE_INFANTRY3_CLIENT_ID;
		}break;	
		case STUDENT_BLUE_INFANTRY4_ID:
		{
			receiver_ID = BLUE_INFANTRY4_CLIENT_ID;
		}break;
		case STUDENT_BLUE_INFANTRY5_ID:
		{
			receiver_ID = BLUE_INFANTRY5_CLIENT_ID;
		}break;
		case STUDENT_BLUE_AERIAL_ID:
		{
			receiver_ID = BLUE_AERIAL_CLIENT_ID;
		}break;
	}
			
	judge_send_mesg.student_interactive_header_data.data_cmd_id = CLIENT_DATA_ID;
	judge_send_mesg.student_interactive_header_data.send_ID = current_robot_id;
	judge_send_mesg.student_interactive_header_data.receiver_ID = receiver_ID;
	memcpy(&judge_send_mesg.student_interactive_header_data.interactive_data[0],frame,sizeof(CLIENT_DATA_LENGTH));

	data_packet_pack(STUDENT_INTERACTIVE_ID, (uint8_t *)&judge_send_mesg.student_interactive_header_data,
									 sizeof(judge_send_mesg.student_interactive_header_data), DN_REG_ID);
}


/**
  * @brief  Pack the robot interactive data
  * @param  content_id:	student content id.
  *			p_data:	pointer to a robot_interactive_data_t structure that
  *					store data.
  */
void judgement_robot_packet_pack(uint16_t content_id, uint8_t *p_data, uint8_t receive_robot_id)
{
	robot_interactive_data_t *frame = (robot_interactive_data_t*)p_data;
	memcpy(frame, p_data, STUDENT_DATA_LENGTH);
	
	uint8_t receiver_ID = 0;
	uint8_t current_robot_id = 0;

	uint8_t *puck_buff;
	
	current_robot_id = judge_recv_mesg.game_robot_state.robot_id;  //读取当前机器人的id
	if(current_robot_id <= 7)
	{
		receiver_ID = receive_robot_id;
	}
	else
	{
		receiver_ID = receive_robot_id + 10;
	}
	switch(content_id)
	{
		
	}
	judge_send_mesg.student_interactive_header_data.data_cmd_id = content_id;
	judge_send_mesg.student_interactive_header_data.send_ID = current_robot_id;
	judge_send_mesg.student_interactive_header_data.receiver_ID = receiver_ID;
	memcpy(&judge_send_mesg.student_interactive_header_data.interactive_data[0],frame,sizeof(frame->data));
	
	data_packet_pack(STUDENT_INTERACTIVE_ID, (uint8_t*)&judge_send_mesg.student_interactive_header_data,
	                 sizeof(judge_send_mesg.student_interactive_header_data), DN_REG_ID);
}











